{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#programming_fever\n",
    "#document scanner using simple python code\n",
    "import numpy as np\n",
    "import cv2\n",
    "import imutils\n",
    "\n",
    "args_image =\"E://OpenCV//docu1.jpg\"\n",
    "\n",
    "image = cv2.imread(args_image)\n",
    "image=cv2.resize(image,(500,500))\n",
    "orig = image.copy()\n",
    "cv2.imshow(\"Original Image\", image)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n",
    "\n",
    "grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
    "grayImageBlur = cv2.blur(grayImage,(2,2))\n",
    "edgedImage = cv2.Canny(grayImageBlur, 100, 300, 3)\n",
    "\n",
    "cv2.imshow(\"gray\", grayImage)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n",
    "cv2.imshow(\"grayBlur\", grayImageBlur)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n",
    "cv2.imshow(\"Edge Detected Image\", edgedImage)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n",
    "\n",
    "allContours = cv2.findContours(edgedImage.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n",
    "allContours = imutils.grab_contours(allContours)\n",
    "allContours = sorted(allContours, key=cv2.contourArea, reverse=True)[:1]\n",
    "\n",
    "perimeter = cv2.arcLength(allContours[0], True) \n",
    "ROIdimensions = cv2.approxPolyDP(allContours[0], 0.02*perimeter, True)\n",
    "cv2.drawContours(image, [ROIdimensions], -1, (0,255,0), 2)\n",
    "cv2.imshow(\"Contour Outline\", image)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n",
    "ROIdimensions = ROIdimensions.reshape(4,2)\n",
    "rect = np.zeros((4,2), dtype=\"float32\")\n",
    "s = np.sum(ROIdimensions, axis=1)\n",
    "rect[0] = ROIdimensions[np.argmin(s)]\n",
    "rect[2] = ROIdimensions[np.argmax(s)]\n",
    "diff = np.diff(ROIdimensions, axis=1)\n",
    "rect[1] = ROIdimensions[np.argmin(diff)]\n",
    "rect[3] = ROIdimensions[np.argmax(diff)]\n",
    "\n",
    "(tl, tr, br, bl) = rect\n",
    "\n",
    "widthA = np.sqrt((tl[0] -tr[0])**2 + (tl[1] - tr[1])**2 )\n",
    "widthB = np.sqrt((bl[0] - br[0])**2 + (bl[1] - br[1])**2 )\n",
    "maxWidth = max(int(widthA), int(widthB))\n",
    "\n",
    "heightA = np.sqrt((tl[0] - bl[0])**2 + (tl[1] - bl[1])**2 )\n",
    "heightB = np.sqrt((tr[0] - br[0])**2 + (tr[1] - br[1])**2 )\n",
    "maxHeight = max(int(heightA), int(heightB))\n",
    "dst = np.array([\n",
    "    [0,0],\n",
    "    [maxWidth-1, 0],\n",
    "    [maxWidth-1, maxHeight-1],\n",
    "    [0, maxHeight-1]], dtype=\"float32\")\n",
    "\n",
    "transformMatrix = cv2.getPerspectiveTransform(rect, dst)\n",
    "scan = cv2.warpPerspective(orig, transformMatrix, (maxWidth, maxHeight))\n",
    "cv2.imshow(\"Scaned\",scan)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n",
    "scanGray = cv2.cvtColor(scan, cv2.COLOR_BGR2GRAY)\n",
    "cv2.imshow(\"scanGray\", scanGray)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n",
    "from skimage.filters import threshold_local\n",
    "T = threshold_local(scanGray, 9, offset=8, method=\"gaussian\")\n",
    "scanBW = (scanGray > T).astype(\"uint8\") * 255\n",
    "cv2.imshow(\"scanned\", scanBW)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
